﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
using DG.Tweening;
public class IceWall : SceneMonoBase
{
    public float MaxAlph = 1;
    public int index;
    public MapEditorBase.MapEditor mapEditor;
    public MapEditorBase.MapWall wall = new MapEditorBase.MapWall();
    public int roomID;
    public MonoBase monoBase;
    public override void OnEnable()
    {
        base.OnEnable();
        StartCoroutine(Init());
    }
    IEnumerator Init()
    {
        yield return 0;
        transform.Find("Sprite").GetComponent<SpriteRenderer>().sortingOrder = 101 - (int)(transform.position.y * 2);
        transform.Find("Sprite").GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
        transform.Find("Sprite").GetComponent<SpriteRenderer>().DOColor(new Color(0.5f, 1, 1, MaxAlph), 0.2f);

        mapEditor = GameMgr.Get<IMapManager>().GetMapEditor();
        wall.param = new List<int>();
        wall.position = this.transform.position;
        wall.sceneType = SceneType.Obstacle;
        roomID = GameMgr.Get<IMapManager>().GetRoomID(GameMgr.Get<IMapManager>().GetNearestGround(wall.position));
        index = mapEditor.mapRooms[roomID].mapWalls.Count;
        mapEditor.mapRooms[roomID].mapWalls.Add(wall);

        if (monoBase == null)
        {
            GameMgr.Get<ITimeManager>().RgstTimeAction(
                Data.Instance.TableAgent.GetFloat("Base", "IceMelt", "Value") - 0.65f, () => {
                    StartCoroutine(Melt());
                });
        }
        else
        {
            while (GameMgr.Get<IMonoManager>().GetMono(monoBase.name).monoRealtimeStatus.IsFrozen > Time.deltaTime)
            {
                yield return 0;
            }
            mapEditor.mapRooms[roomID].mapWalls.Remove(wall);
            GameMgr.Get<ISceneMonoManager>().RemoveSceneMono(gameObject.name);
        }
    }
    IEnumerator Melt()
    {
        yield return 0;
        transform.Find("Sprite").GetComponent<SpriteRenderer>().DOColor(new Color(1, 1, 1, 0), 0.25f);
        yield return new WaitForSeconds(0.6f);
        mapEditor.mapRooms[roomID].mapWalls.Remove(wall);
        GameMgr.Get<ISceneMonoManager>().RemoveSceneMono(gameObject.name);
    }
}
